package com.example.demo.utils;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLSelectItem;
import com.alibaba.druid.sql.ast.statement.SQLSelectQuery;
import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.util.JdbcConstants;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class ParserUtils {
    ObjectMapper  json = new ObjectMapper();
    public static void main(String[] args) throws JsonProcessingException {
        ParserUtils parserUtils = new ParserUtils();
        parserUtils.helloWorld();
//        parserUtils.updateMappingTableName();


    }
    public void helloWorld() throws JsonProcessingException {
        String sql = "select user.id,user.name from user user left join student student on user.id = student.id where 1 = 1";
        // 新建 MySQL Parser
        SQLStatementParser parser = new MySqlStatementParser(sql);

        // 使用Parser解析生成AST，这里SQLStatement就是AST
        SQLStatement statement = parser.parseStatement();

        // 使用visitor来访问AST
        MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
        statement.accept(visitor);

        // 从visitor中拿出你所关注的信息
        System.out.println(visitor.getColumns());
        for(int i = 0;i<visitor.getConditions().size();i++){
            String s = visitor.getConditions().get(i).toString();
//            System.out.println(s);

        }
//        System.out.println(json.writeValueAsString(visitor.getConditions().get(0).toString()));
//        System.out.println(json.writeValueAsString(visitor.getTables()));

        // 可以是ORACLE、POSTGRESQL、SQLSERVER、ODPS等
        final String dbType = JdbcConstants.MYSQL;

        String sql2 = "select * from t";
        List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
        System.out.println(stmtList.size());
        for(int i = 0;i<stmtList.size();i++){
            SQLSelectStatement sqlStatement = (SQLSelectStatement) stmtList.get(i);
            SQLSelect select = sqlStatement.getSelect();
            SQLSelectQuery query = select.getQuery();


            Map<String, Object> attributes = query.getAttributes();
            System.out.println(attributes);

            System.out.println("--->"+sqlStatement);

            List<SQLObject> children = sqlStatement.getChildren();
            System.out.println(children.size());
            SQLObject sqlObject = children.get(0);
            System.out.println();;

        }
    }
    public void updateMappingTableName(){
        Map<String, String> mapping = Collections.singletonMap("user", "user_01");

        String sql = "select * from user";
        String result = SQLUtils.refactor(sql, JdbcConstants.MYSQL, mapping);
        System.out.println("result : "+result);
    }
    public void zuzhuangSelect(){
        SQLSelect sqlSelect = new SQLSelect();
        SQLSelectQuery sqlSelectQuery = new MySqlSelectQueryBlock();
        List<SQLSelectItem>  selectList      = new ArrayList<SQLSelectItem>();
        SQLSelectItem sqlSelectItem = new SQLSelectItem();

    }
}
